package com.drinks.yiniang.ui.activity.user.setting;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.user.UserInfoJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.ui.widget.textview.BorderTextView;

/**
 * 用户选择姓名页面
 *
 * @author han
 * @date 2019/09/08
 */
public class ChooseSexActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    //nav
    private NavigationBarLayout nav_bar_choose_sex;
    //男
    private BorderTextView tv_choose_sex_boy;
    //女
    private TextView tv_choose_sex_girl;

    //当前性别
    private String mCurrentSex;

    @Override
    protected void initView() {
        nav_bar_choose_sex = findViewById(R.id.nav_bar_choose_sex);

        tv_choose_sex_boy = findViewById(R.id.tv_choose_sex_boy);
        tv_choose_sex_girl = findViewById(R.id.tv_choose_sex_girl);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        switchSexStatus(Constants.SEX_BOY);
    }

    @Override
    protected void initListener() {
        nav_bar_choose_sex.setOnNavgationBarClickListener(this);
        tv_choose_sex_boy.setOnClickListener(this);
        tv_choose_sex_girl.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tv_choose_sex_boy://男
                switchSexStatus(Constants.SEX_BOY);
                break;
            case R.id.tv_choose_sex_girl://女
                switchSexStatus(Constants.SEX_GIRL);
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_choose_sex;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {//完成
        doModifyUserInfo();
    }

    private void switchSexStatus(@NonNull String sex) {
        if (sex.equals(mCurrentSex)) {
            return;
        }

        mCurrentSex = sex;

        if (sex.equals(Constants.SEX_BOY)) {//切换到选择男
            tv_choose_sex_boy.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.mipmap.picker_orignal_checked, 0);
            tv_choose_sex_girl.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.mipmap.picker_orignal_normal, 0);
        } else {//切换到选择女
            tv_choose_sex_boy.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.mipmap.picker_orignal_normal, 0);
            tv_choose_sex_girl.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.mipmap.picker_orignal_checked, 0);
        }
    }

    private void doModifyUserInfo() {
        showLoadingDialog();

        UserApiIO.getInstance().modifyUserInfo("sex", mCurrentSex, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                Intent intent = new Intent();
                intent.putExtra(Constants.KeyTextContents, mCurrentSex);
                setResult(Constants.RESULT_CODE_NORMAL, intent);
                finish();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
